package com.challenger.admin.action;

import java.util.HashMap;
import java.util.List;
import java.util.UUID;

import org.apache.log4j.Logger;

import com.challenger.admin.entity.User;
import com.challenger.admin.services.UserService;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionSupport;

public class UserAction extends ActionSupport {

	/**
	 * 
	 */
	private static final long serialVersionUID = -612055470788299594L;
	private static Logger logger = Logger.getLogger(UserAction.class);
	private User user;
	private UserService userService;
	HashMap<String, Object> jsonMap;
	
	public String addUser()
	{
		logger.info("开始添加用户");
		//一些非空处理、省略
		logger.info("User [userId=" + user.getUserId() + ", userNo=" + user.getUserNo() + ", userName=" + user.getUserName() + ", userSex=" + user.getUserSex() + ", userAge=" + user.getUserAge() + "]");
		String uuid = UUID.randomUUID().toString().replace("-", "");
		user.setUserId(uuid);
		logger.info("User [userId=" + user.getUserId() + ", userNo=" + user.getUserNo() + ", userName=" + user.getUserName() + ", userSex=" + user.getUserSex() + ", userAge=" + user.getUserAge() + "]");
		User existUser = userService.queryUserByNo(user);
		if(existUser != null){
			jsonMap = new HashMap<String, Object>();
			jsonMap.put("msg", "add User failure. User already exists.");
			logger.info("User [userId=" + user.getUserId() + ", userNo=" + user.getUserNo() + ", userName=" + user.getUserName() + ", userSex=" + user.getUserSex() + ", userAge=" + user.getUserAge() + "] already exits.");
			return Action.INPUT;
		}
		logger.info("User [userId=" + user.getUserId() + ", userNo=" + user.getUserNo() + ", userName=" + user.getUserName() + ", userSex=" + user.getUserSex() + ", userAge=" + user.getUserAge() + "] not exist, Continue to insert User...");
		int successflag = userService.insertUser(user);
		logger.info("插入用户个数：" + successflag);
		if(successflag > 0){
			User usr = userService.queryUserByNo(user);
			jsonMap = new HashMap<String, Object>();
			jsonMap.put("user", usr);
			jsonMap.put("msg", "add User success.");
		}else{
			jsonMap = new HashMap<String, Object>();
			jsonMap.put("msg", "add User failure.");
		}
		logger.info("User [userId=" + user.getUserId() + ", userNo=" + user.getUserNo() + ", userName=" + user.getUserName() + ", userSex=" + user.getUserSex() + ", userAge=" + user.getUserAge() + "] 添加完成...");
		return Action.INPUT;
	}
	
	public String queryAllUser()
	{
		List<User> userList = userService.queryAllUser();
		jsonMap = new HashMap<String, Object>();
		jsonMap.put("msg", "query User success.");
		jsonMap.put("list", userList);
		return Action.INPUT;
	}
	
	public String updateUser()
	{
		return Action.SUCCESS;
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public UserService getUserService() {
		return userService;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	public HashMap<String, Object> getJsonMap() {
		return jsonMap;
	}

	public void setJsonMap(HashMap<String, Object> jsonMap) {
		this.jsonMap = jsonMap;
	}
}
